package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.constant.state.BizLogType;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.system.model.LoginLog;
import cn.stylefeng.guns.modular.system.model.OperationLog;
import cn.stylefeng.guns.modular.system.warpper.LogWarpper;
import cn.stylefeng.guns.modular.system.warpper.OrderWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Order;
import cn.stylefeng.guns.modular.order.service.IOrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 *
 * @author fengshuonan
 * @Date 2020-04-26 16:59:35
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/mall/order/";

    @Autowired
    private IOrderService orderService;

    /**
     * 跳转到订单首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sys_order.html";
    }

    /**
     * 跳转到添加订单
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "sys_order_add.html";
    }

    /**
     * 跳转到修改订单
     */
    @RequestMapping("/order_update/{orderId}")
    public String orderUpdate(@PathVariable String orderId, Model model) {
        Order order = orderService.selectById(orderId);
        model.addAttribute("item",order);
        LogObjectHolder.me().set(order);
        return PREFIX + "sys_order_edit.html";
    }

    /**
     * 获取订单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String state,String orderid) {
        Integer userid = ShiroKit.getUser().getId();
        if(!ShiroKit.getUser().getRoleList().contains(6)){
            userid = 999999999;
        }
        Page<Order> page = new PageFactory<Order>().defaultPage();
        List<Map<String, Object>> result = orderService.list(page, condition, state, userid, orderid);
        page.setRecords(new OrderWrapper(result).wrap());
        return new PageInfoBT<>(page);

    }

    /**
     * 新增订单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        orderService.insert(order);
        return SUCCESS_TIP;
    }

    /**
     * 删除订单
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer orderId) {
        orderService.deleteById(orderId);
        return SUCCESS_TIP;
    }

    /**
     * 订单发货
     */
    @RequestMapping(value = "/shipping")
    @ResponseBody
    public Object shipping(@RequestParam String orderId) {
        Order order1 = orderService.selectById(orderId);
        order1.setState("66");
        orderService.updateById(order1);
        return SUCCESS_TIP;
    }

    /**
     * 修改订单
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Order order,@RequestParam(required = false) Integer flag) {
        Order order1 = order;
        if(ToolUtil.isNotEmpty(flag)){
            order1 = orderService.selectById(order.getId());
            switch(flag){
                case 2:
                    order1.setState(order.getState());
                    order1.setRemindnum(order.getRemindnum());
                    break;
                default:
                    order1.setState(order.getState());
            }
            order1.setState(order.getState());
        }
        orderService.updateById(order1);
        return SUCCESS_TIP;
    }

    /**
     * 订单详情
     */
    @RequestMapping(value = "/detail/{orderId}")
    @ResponseBody
    public Object detail(@PathVariable("orderId") Integer orderId) {
        return orderService.selectById(orderId);
    }

    /**
     * 付款订单
     */
    @RequestMapping(value = "/pay")
    public String pay(String orderid) {
        Order order1 = orderService.selectById(orderid);
        order1.setState("65");
        orderService.updateById(order1);
        return "redirect:/order/pay_success";
    }

    /**
     * 付款订单
     */
    @RequestMapping(value = "/pay_success")
    public String paySuccess() {
        return "/mall/pay_success.html";
    }

    @RequestMapping("/evaluate/{orderId}")
    public String orderEvaluate(@PathVariable String orderId, Model model) {
        Order order = orderService.selectById(orderId);
        model.addAttribute("item",order);
        model.addAttribute("isEvaluate",true);
        LogObjectHolder.me().set(order);
        return PREFIX + "user_order_evaluate.html";
    }
}
